<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
if($_RQ['store_id']){
	$this->session('store_id',$store_id);
}
$store_id=$this->session('store_id');
if(!$store_id){
	$store_id=pdo_getcolumn('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'id');
	$this->session('store_id',$store_id);
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
if($_RQ['op']=='list'){
	$serve_list=pdo_getall('jy_qiuyuyue_store_serve',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','display asc');
	foreach($serve_list as &$item){
		//已支付，已确认，已完成订单数
		$server=$this->pdo_get('serve',array('id'=>$item['serve_id']));
		$item=array_merge($server,$item);
		$num1=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$item['id'],'status'=>'1'));
		$num2=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$item['id'],'status'=>'2'));
		$num3=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$item['id'],'status'=>'4'));
		$item['num']=$num1+$num2+$num3;
	}
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);
	$return['post']=array();
	$return['serve_list']=$serve_list;
	$return['sharedata']=array(
			'title'=>'项目列表',
			'desc'=>$basic['share_desc']?:'球场预定，在线预定足球场，羽毛球场！',
			'link'=>custom_url('jy_qiuyuyue/server//fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	exi($return);
}
if($_RQ['op']=='sever_detail'){
	if(!$_RQ['id']){
		exi('没有找到该项目！','error');
	}
	//查询项目具体信息
	$serve=pdo_get('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$package=$serve['package'];
	//查询本项目可服务员工(未锁定，状态为可服务)
	$store_id=$this->session('store_id');
	$worker_list=pdo_getall('jy_qiuyuyue_store_worker',array('store_id'=>$store_id,'uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	foreach($worker_list as $k=>$item){
		$service=explode(";",$item['service']);
		if(!in_array($_RQ['id'],$service)){
			unset($worker_list[$k]);
		}
	}
	//查询自定义字段
	$fileds=$serve['fileds']?json_decode($serve['fileds']):array();
	//查询本项目的访问人数及添加访问人数
	pdo_update('jy_qiuyuyue_serve',array('num +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//已支付，已确认，已完成订单数
	$num1=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$_RQ['id'],'status'=>'1'));
	$num2=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$_RQ['id'],'status'=>'2'));
	$num3=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$_RQ['id'],'status'=>'4'));
	$serve['ordernum']=$num1+$num2+$num3;
	$serve_time=json_decode($serve['serve_time'],true);
	//日期 今天，未来七天
	$data=array();
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	$days=$basic['days']>0?$basic['days']:7;
	$yue0=0;
	if($basic['no_yueday']){
		$days++;
		$yue0=1;
	}
	for($i=$yue0;$i<$days;$i++){
		$data[]=array('riqi'=>date("m-d",strtotime("+".$i." day")),'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))))],'isshow'=>($serve['advancedays']>0&&$i<$serve['advancedays'])?-1:1);
	}
	$weekid=date("w");
	$weekid=$weekid?:7;
	$iswork=strpos($serve['work_time'],';'.$weekid.';');	
	$this->session('serve_id',$_RQ['id']);
	$serve['hdps']=json_decode($serve['hdps'],true);
	$return['serve']=$serve;
	$return['plate']=pdo_getall('jy_qiuyuyue_serve_plate',array('serve_id'=>$serve['id'],'uniacid'=>$_SESSION['uniacid']),'','paixu desc');
	if(!$return['plate'][0]['id']){
		exi('该项目还未开启，未添加任何场地！','error');
	}
	$return['serve_time']=array();
	$return['data']=$data;
	$return['post']=array(
		'dayid'=>0,
		'isye'=>0,
		'timeid'=>-1,
		'price'=>0,
		'serve_id'=>$serve['id'],
		'store_id'=>$store_id,
		'plate_id'=>$return['plate'][0]?$return['plate'][0]['id']:0,
		'users'=>array(),
		'total'=>1
	);
	$return['dingxieyi']=$this->plugin_setting('dingxieyi');
	$return['sharedata']=array(
			'title'=>'项目-'.$serve['title'],
			'desc'=>$serve['remark']?:'足球馆预约！',
			'link'=>custom_url('jy_qiuyuyue/server/sever_detail/id='.$serve['id'].'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$serve['image'],
	);
	exi($return);
}
if($_RQ['op']=='serve_time'){
	$postdata=get_postdata();
	$isye=intval($postdata['isye']);
	$is_ban=intval($postdata['is_ban']);
	$serve_id=intval($postdata['serve_id']);
	if(!isset($postdata['serve_id'])){
		exi('请选择项目','error');
	}
	$dayid=intval($postdata['dayid']);
	if(!isset($postdata['dayid'])){
		exi('请重新选择日期','error');
	}
	$plate_id=intval($postdata['plate_id']);
	if(!$plate_id){
		exi('请选择场地！','error');
	}
	$data=array();
	$data=$this->days($_RQ['id'],$basic['days']);
	$return['data']=$data;
	$post=get_postdata();
	$serve=pdo_get('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve_id));
	$work_time=$serve['work_time'];
	$date=$data[$dayid]['ymd'];
	$daytime=strtotime($date);
	$weekid=date("w",$daytime);
	$weekid=$weekid?:7;
	$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$date,'serveid'=>$serve_id,'uniacid'=>$_SESSION['uniacid']));
	if($onetime['serve_time']){
		$serve_time=$onetime['serve_time'];
		$iswork=$onetime['is_close']?0:1;
	}else{
		$iswork=strpos($work_time,';'.$weekid.';')?1:0;
		$serve_time=$serve['serve_time'];
	}
	$serve_time=json_decode($serve_time,true);
	//获取当前时间
	$time=time();
	if($serve_time){
		foreach($serve_time as $k=>&$sv){
			if($is_ban){
				$sv['id1']=$k.'A';
				$sv['id2']=$k.'B';
			}else{
				$sv['id']=$k.'C';
			}
		}
		$yetime=mktime(18,00,0,date('m'),date('d'),date('Y'));
		foreach($serve_time as $k=>&$item){
			$day=date('Y-m-d',TIMESTAMP);
			$starttime=strtotime($day.' '.$item['times']['start']);
			$item['starttime']=$starttime;
			$item['yetime']=$yetime;
			$item['day']=$date;
			if($isye && $starttime<$yetime){
				$item['ispass']=1;
			}
			if(!$isye && $starttime>=$yetime){
				$item['ispass']=1;
			}
			$item['iswork']=$iswork;
			$item['weekid']=$weekid;
			$item['time']=time();
			//时间转为时间戳
			$item['shijian']=strtotime($data[$dayid]['ymd'].$item['times']['start']);
			if($item['shijian']<time() || !$iswork){
				$item['show']=0;
			}
			else{
				$item['show']=1;
			}
			$day=$data[$dayid]['ymd'];
			if($is_ban){
				$timeid=$k.'C';
				$res=$this->model('order')->checktimeid($is_ban,$timeid,$store_id,$day,$serve_id,$plate_id,$serve['open_total'],$item['total']);
				if($res){
					$item['lastnum']=$res;
					$timeid=$item['id1'];
					$res=$this->model('order')->checktimeid($is_ban,$timeid,$store_id,$day,$serve_id,$plate_id,$serve['open_total'],$item['total']);
					$item['yuyue1']=$res?1:0;
					$timeid=$item['id2'];
					$res=$this->model('order')->checktimeid($is_ban,$timeid,$store_id,$day,$serve_id,$plate_id,$serve['open_total'],$item['total']);
					$item['yuyue2']=$res?1:0;
					$item['yuyue']=$item['yuyue1'] || $item['yuyue2']?1:0;
				}else{
					$item['yuyue1']=0;
					$item['yuyue2']=0;
					$item['yuyue']=0;
				}
			}else{
				$timeid=$k.'C';
				$res=$this->model('order')->checktimeid($is_ban,$timeid,$store_id,$day,$serve_id,$plate_id,$serve['open_total'],$item['total']);
				if($serve['open_total']){
					$item['lastnum']=$res;
				}
				if($res){
					$timeid=$k.'A';
					$res=$this->model('order')->checktimeid($is_ban,$timeid,$store_id,$day,$serve_id,$plate_id,$serve['open_total'],$item['total']);
				}
				if($res){
					$timeid=$k.'B';
					$res=$this->model('order')->checktimeid($is_ban,$timeid,$store_id,$day,$serve_id,$plate_id,$serve['open_total'],$item['total']);
				}
				$item['yuyue']=$res?1:0;
			}
			$item['fee']=$item['fee']?:$serve['price'];
			if($weekid==6 && $serve['week6_addfee']>0){
				if($isye && $serve['week6_addtime']!=1){
					$item['fee'] +=$serve['week6_addfee'];
				}
				if(!$isye && $serve['week6_addtime']!=2){
					$item['fee'] +=$serve['week6_addfee'];
				}
			}
			if($weekid==7 && $serve['week7_addfee']>0){
				if($isye && $serve['week7_addtime']!=1){
					$item['fee'] +=$serve['week7_addfee'];
				}
				if(!$isye && $serve['week7_addtime']!=2){
					$item['fee'] +=$serve['week7_addfee'];
				}
			}
			if($is_ban){
				$item['fee']=50*$item['fee']/100;
			}
		}
	}
	$return['serve_time']=$serve_time;
	$return['success']=1;
	exi($return);
}
if($_RQ['op']=='check_buy'){
	$store_id=$_RQ['store_id']?:$store_id;
	$is_ban=intval($_RQ['is_ban']);
	if(!$_RQ['plate_id']){
		exi('请选择场地','error');
	}
	if(!$_RQ['timeids']){
		exi('请选择场次!','error');
	}
	//已支付，已确认，已完成订单数
	//日期 今天，未来七天
	$data=array();
	$days=$basic['days']>0?$basic['days']:7;
	$yue0=0;
	if($basic['no_yueday']){
		$days++;
		$yue0=1;
	}
	for($i=$yue0;$i<$days;$i++){
		$data[]=date("Y-m-d",strtotime("+".$i." day"));
	}
	$timeids=str_array($_RQ['timeids']);
	if(!count($timeids)){
		exi('请选择场次!','error');
	}
	$day=$data[$_RQ['dayid']];
	$open_total=pdo_getcolumn('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['serve_id']),'open_total');
	$num=count(str_array($_RQ['users']));
	$num++;
	$serve=pdo_get('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['serve_id']),array('id','title','price','serve_time','package','serve_types','week6_addfee','week7_addfee','week6_addtime','week7_addtime'));
	$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$day,'serveid'=>$_RQ['serve_id'],'uniacid'=>$_SESSION['uniacid']));
	if($onetime['serve_time']){
		$serve['serve_time']=$onetime['serve_time'];
	}
	$serve['serve_time']=json_decode($serve['serve_time'],true);
	foreach($timeids as $k=>$tv){
		$timekey=substr($tv, 0, -1);
		$total=$serve['serve_time'][intval($timekey)]['total'];
		$res=$this->model('order')->checktimeid($is_ban,$tv,$store_id,$day,$_RQ['serve_id'],$_RQ['plate_id'],$open_total,$total);
		if($open_total){
			if($res<$num){
				exi($tv.'该场次已被预约满了！','error');
			}
		}else{
			if(!$res){
				exi($tv.'该场次已被预约了！','error');
			}
		}
	}
	exi('','','jy_qiuyuyue/book//is_ban='.$is_ban.'&store_id='.$store_id.'&serve_id='.$_RQ['serve_id'].'&plate_id='.$_RQ['plate_id'].'&timeids='.$_RQ['timeids'].'&dayid='.$_RQ['dayid'].'&users='.$_RQ['users']);
}
if($_RQ['op']=='package'){
	if(!isset($_RQ['id'])){
		exi('请重新选择项目','error');
	}
	//exi($_RQ['id'],'error');
	$package=pdo_getcolumn('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'package');
	$package=json_decode($package,true);
	//查询自定义字段
	$fileds=pdo_getcolumn('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'fileds');
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['package']=$package;
	$return['success']=1;
	exi($return);
}
if($_RQ['op']=='upusers'){
	$post=get_postdata();
	$myuser=$post['myuser'];
	$num=count(str_array($post['users']));
	$num++;
	pdo_update('jy_qiuyuyue_member',array('myuser'=>ijson_encode($myuser)),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['total']=$num;
	exi($return);
}
if($_RQ['op']=='get_workers'){
	if(!$_RQ['id']){
		exi('请选择项目！','error');
	}
	if(!$_RQ['store_id']){
		exi('请选择场馆！','error');
	}
	$worker_list=pdo_getall('jy_qiuyuyue_store_worker',array('store_id'=>$_RQ['store_id'],'uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	foreach($worker_list as $k=>$item){
		$service=explode(";",$item['service']);
		if(!in_array($_RQ['id'],$service)){
			unset($worker_list[$k]);
		}
	}
	$return['list']=$worker_list;
	exi($return);
}
if($_RQ['op']=='get_store'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}else{
		$lat_lng=$this->session('lat_lng');
	}
	//场馆信息
	$store=pdo_get('jy_qiuyuyue_store',array('id'=>$store_id));
	$store['images']=json_decode($store['images'],true);
	$store['images'][0]=$store['images'][0]?:'../core/resource/images/nopic.jpg';
	$store['image']=$store['images'][0];
	$store['time']=json_decode($store['time'],true);
	$store['service']=json_decode($store['service'],true);
	$store['lat_lng']=json_decode($store['lat_lng'],true);
	if($lat_lng){
		$store['distance']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$store['lat_lng']['lng'],$store['lat_lng']['lat']);
		$store['distance']=intval($v['distance']/1000);
	}
	$return['store']=$store;
	exi($return);
}